package com.rs.consumer;

import cn.hutool.core.bean.BeanUtil;
import com.rabbitmq.client.Channel;
import com.rs.cache.QueueName;
import com.rs.command.CommandBuffer;
import com.rs.entity.task.Command;
import com.rs.util.LogUtil;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * 接收RCS发送的命令(命令消费者)
 *
 * @author zhouwz
 * @date 2020/9/14 14:47
 */
@Component
public class CommandConsumer {

    @RabbitListener(queues = QueueName.SEND_COMMAND_SIMULATOR)
    public void handler(Map<String, Object> map, Channel channel, Message message) throws IOException {
        try {
            Command command = BeanUtil.mapToBean(map, Command.class, true);
            LogUtil.logFormat(command);
            // 接收上层发送到的指令
            CommandBuffer.receive(command.getDeviceNo(), command);
        } catch (Exception e) {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            e.printStackTrace();
        }
    }
}